package com.ruoyi.file.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.IDCardValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.file.domain.IdCardInfo;
import com.ruoyi.file.service.IIdCardInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

@Api(value = "身份证信息", tags = "身份证信息Controller")
@RestController
@RequestMapping("/idCard")
public class IdCardInfoController extends BaseController {
    @Autowired
    private IIdCardInfoService idCardInfoService;

    /**
     * 查询身份证信息列表
     */
    @RequiresPermissions("file:info:list")
    @GetMapping("/list")
    public TableDataInfo list(IdCardInfo idCardInfo) {
        startPage();
        List<IdCardInfo> list = idCardInfoService.selectIdCardInfoList(idCardInfo);
        return getDataTable(list);
    }

    /**
     * 导出身份证信息列表
     */
    @RequiresPermissions("file:info:export")
    @Log(title = "身份证信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IdCardInfo idCardInfo) {
        List<IdCardInfo> list = idCardInfoService.selectIdCardInfoList(idCardInfo);
        ExcelUtil<IdCardInfo> util = new ExcelUtil<IdCardInfo>(IdCardInfo.class);
        util.exportExcel(response, list, "身份证信息数据");
    }

    /**
     * 获取身份证信息详细信息
     */
    @RequiresPermissions("file:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(idCardInfoService.selectIdCardInfoById(id));
    }

    @ApiOperation("获取跟单信息详细信息getInfoById")
    @GetMapping("/getPersonByIdCard")
    public AjaxResult getPersonByIdCard(@ApiParam(value = "身份证号码", required = true) @RequestParam(value = "idCard") String idCard) {
        return success(idCardInfoService.getPersonByIdCard(idCard));
    }


    /**
     *
     */
/*
    @RequiresPermissions("file:info:add")
    @Log(title = "身份证信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IdCardInfo idCardInfo) {
        return toAjax(idCardInfoService.insertIdCardInfo(idCardInfo));
    }
*/

    /**
     * 修改身份证信息
     */
    @ApiOperation("新增 修改 身份证信息")
    @RequiresPermissions("file:info:edit")
    @Log(title = "身份证信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody@Validated IdCardInfo idCardInfo) {
        if( ! IDCardValidator.isValidIDCard( idCardInfo.getIdCard())){
            return error(  "身份证号码不对");
        }

        return toAjax(idCardInfoService.updateIdCardInfo(idCardInfo));
    }

    /**
     * 删除身份证信息
     */
    @RequiresPermissions("file:info:remove")
    @Log(title = "身份证信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(idCardInfoService.deleteIdCardInfoByIds(ids));
    }
}
